from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
# from autoTest_ecshop.utils

class Base():
    def __init__(self,driver):
        self.driver = driver

    # 查找单个元素
    def find_element(self,locator):
        try:
            print(f"查找元素 {locator}")
            return  WebDriverWait(self.driver,20). \
                until(EC.visibility_of_element_located(locator))
        except Exception as e:
            print(f"元素 {locator} 定位失败。失败原因是：\n {e}")

    # 查找多个元素
    def find_elements(self, locator):
        try:
            return WebDriverWait(self.driver, 20). \
                until(EC.visibility_of_all_elements_located(locator))
        except Exception as e:
            print(f"元素 {locator} 定位失败。失败原因是：\n {e}")

    # 输入数据
    def input_data(self,locator,keywords):
        # 查找元素
        element = self.find_element(locator)
        # 清空输入框的内容
        element.clear()
        # 输入数据
        print(f"输入数据: {keywords}")
        element.send_keys(keywords)

    def clickBtn(self,locator):
        # 查找元素
        button = self.find_element(locator)
        # 点击按钮
        # print(f)




        # def switchAlert(self, keywords=None, text=True,action=True):
        #     if action:
        #         alert.accept()
        #     else:
        #         # 点击取消按钮
        #